use super::jit_interface::RustPhpJitInterface;

use std::mem;

pub struct VirtualMachine {}

extern crate rustphp_parser;

impl VirtualMachine {
    pub fn new() -> VirtualMachine {
        let vm = VirtualMachine {};

        vm
    }

    /// 运行脚本
    pub fn run_script(&self, script: &str) {
        match rustphp_parser::php::PHPParser::new().parse(script) {
            Ok(result) => {
                let mut jit = RustPhpJitInterface::new();
                let main_function = jit.compile(result).unwrap();

                let main_function = unsafe { mem::transmute::<_, fn() -> isize>(main_function) };
                main_function();
            }
            Err(e) => println!("{:?}", e),
        }
        println!();
    }
}
